define(['angular',"text!www/trends/trends_new.html",'url','common','swiper','jqload'], function (angular,tpl) {
    //angular会自动根据controller函数的参数名，导入相应的服务
   function controller($timeout,$scope,$rootScope,$http,trendsService){
	   initFooter(3);
	   showSpin();
	   bottomMenu();
	   $rootScope.notify = cl.user.notify;
	  // console.log(cl.user.notify);
	   trendsService.tabId=="newest";
	   trendsService.pageNew = 2;
	   trendsService.pageNewTop = 2;
	   $scope.trends = trendsService;
	   $scope.trends.initTrends = function(){
	    	 if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	         this.busy = true;
	         var pro = this;
	         var url = cl.getTrendsNewUrl+"&callback=JSON_CALLBACK&catId="+this.catId;
	         $http.jsonp(url).success(function (res) {
	        	 var pageTimes = (new Date()).valueOf();
					if(res.code=="CL02023"){//获取成功
						hideSpin();
						cl.user.notify = res.result.notify;
						$rootScope.notify = cl.user.notify ;
						pro.banners = res.result.ad;
						pro.newsCat = res.result.newsCat;
						pro.newest = res.result.newest;
						for (var i = 0; i < pro.newest.length; i++) {
			            	 pro.newest[i].lazy = "lazy"+pageTimes;
			             }
						pro.news_top = res.result.news_top;
						for (var i = 0; i < pro.news_top.length; i++) {
			            	 pro.news_top[i].lazy = "lazy"+pageTimes;
			            }
						pro.wxid = res.result.wxid;
						trendsService.setTrends(pro);
					}
					$timeout(function(){
						initBox();
						lazyloadPage("lazy"+pageTimes);
						initClick();
						btnScroll();
					},1000);
					
					
					
					pro.busy = false;
	         });
	     };
	     
	     $scope.trends.changeCat = function(catId,index){//切换类似
		 		showSpin();
		 		this.newest = [];
				this.news_top = [];
				this.catId = catId;
				this.catAllActive = "";
				for(var k in this.newsCat){
	    			this.newsCat[k].active="";
		    	}
				if(index!="ALL")
				{
					this.newsCat[index].active = "active";
				}else{
					this.catAllActive = "active";
				}
				trendsService.changeCatData(this);
				this.reSet = 1;
				if(this.tabId=="newest"){
					this.nextPage();
				}else if(this.tabId="newstop"){
					this.nextPageTop();
				}
		 	};
		 	$scope.trends.changeStatus = function(status){//切换类似
		 		this.tabId = status;
		 		trendsService.setTabId = status;
		 	};
		 	
	     $scope.trends.likeNew = function(news_id,index){
		    	if(!this.wxid){
		    		showAlert("登录后方可点赞,3秒后将自动登录");
		    		setTimeout("window.location.href=cl.wxLoginUrl;",3000);
		    		
		    		return false;
		    	}
		    	if (this.busy){
			       	 hideSpin();
			       	 return;
			        }
			    this.busy = true;
			    
			    
			    
			    var pro = this;
			    var url = cl.likeNewUrl+"&wxid="+this.wxid +"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
			    $http.jsonp(url).success(function (res) {
					if(res.code=="CL02036"){//获取成功
						if(res.result.codeMsg=="cancel"){
							if(pro.tabId=="newest"){
								pro.newest[index].like_num =  parseInt(pro.newest[index].like_num) - 1;
								pro.newest[index].is_like = "";
								trendsService.setNewest(pro.newest);
							}else if(pro.tabId=="newstop"){
								pro.news_top[index].like_num =  parseInt(pro.news_top[index].like_num) - 1;
								
								pro.news_top[index].is_like = "";
								trendsService.setNewsTop(pro.news_top);
							}
							showAlert("取消点赞");
						}else{
							if(pro.tabId=="newest"){
								pro.newest[index].like_num = parseInt(pro.newest[index].like_num) + 1;
								pro.newest[index].is_like = "liked";
								trendsService.setNewest(pro.newest);
							}else if(pro.tabId=="newstop"){
								pro.news_top[index].like_num = parseInt(pro.news_top[index].like_num) + 1;
								pro.news_top[index].is_like = "liked";
								trendsService.setNewsTop(pro.news_top);
								
							}
							showAlert("成功点赞");
						}
					}else{
						showAlert("点赞失败，请登录后再操作");
					}
					pro.busy = false;
			    });
		    };
	     $scope.trends.collectNews = function(news_id,index){
	    	 if (this.busy){
	        	 return;
	         }
	    	 if(!this.wxid>0)
	         {
	    		 //showAlert("登录后方可收藏");
	    		 showAlert("登录后方可收藏,3秒后将自动登录");
	    		 setTimeout("window.location.href=cl.wxLoginUrl;",3000);
		    		
	    		 return;
	         }
	    	 this.busy = true;
	    	 var pro = this;
	         var url = cl.collectNewUrl+"&wxid="+this.wxid+"&news_id="+news_id+"&callback=JSON_CALLBACK" ;
	         $http.jsonp(url).success(function (res) {
					if(res.code=="CL02026"){//获取成功
						if(res.result.codeMsg=="cancel"){
							if(pro.tabId=="newest"){
								pro.newest[index].collect_times =  parseInt(pro.newest[index].collect_times) - 1;
								pro.newest[index].is_collect = " ";
								trendsService.setNewest(pro.newest);
							}else if(pro.tabId=="newstop"){
								pro.news_top[index].collect_times =  parseInt(pro.news_top[index].collect_times) - 1;
								pro.news_top[index].is_collect = " ";
								trendsService.setNewsTop(pro.news_top);
							}
							showAlert("取消收藏");
						}else{
							if(pro.tabId=="newest"){
								pro.newest[index].collect_times =  parseInt(pro.newest[index].collect_times) + 1;
								pro.newest[index].is_collect = "collected";
								trendsService.setNewest(pro.newest);
							}else if(pro.tabId=="newstop"){
								pro.news_top[index].collect_times =  parseInt(pro.news_top[index].collect_times) + 1;
								pro.news_top[index].is_collect = "collected";
								trendsService.setNewsTop(pro.news_top);
							}
							showAlert("已加入收藏");
						}
					}else{
						showAlert("收藏失败，请登录后再操作");
						
					}
					pro.busy = false;
	         });
	     };
	     
	     $scope.trends.seeImg = function(newindex,index){
		 		var pro = this;
		 		if(pro.tabId=="newest"){
		 			var items = pro.newest[newindex].thumb;
		 		}else if(pro.tabId=="newstop"){
		 			var items = pro.news_top[newindex].thumb;
		 		}
		 		var urls = [];
		 		for (var i = 0; i < items.length; i++) {
		 			urls[i] =pro.base+items[i].img;
		 		}
		 		wx.previewImage({
		 		    current: pro.base+items[index].img, // 当前显示图片的http链接
		 		    urls: urls // 需要预览的图片http链接列表
		 		});
		 	};
	 	$scope.trends.nextPage = function(){//上拉刷新
	 		if(this.tabId=="newstop"){
	 			return false;
	 		}
	 		if(!isFooter()){
	 			if(!this.reSet){
	 				return ;
	 			}
	 		}
	 		if (this.busy){
	        	 hideSpin();
	        	 return;
	         }
	 		this.busy = true;
	 		var pro = this;
	 		
	 		
	 		
	 		var endTime = "";
	 		if(!this.reSet){//简单刷新
	 			if(this.tabId=="newest"){
	 				var item = this.newest;
	 				
	 				var page = trendsService.pageNew;
	 				trendsService.pageNew++;
	 			}else if(this.tabId=="newstop"){
	 				var item = this.news_top;
	 				var page = trendsService.pageNewTop;
	 				trendsService.pageNewTop++;
	 			}
	 			if(item.length>0){
	 	    	   site = parseInt(item.length)-1;
	 	    	   var endTime = item[site].create_time;
	 	       }else{
	 	    	   var endTime = "";
	 	       }
	 		}
	 		
	 		
	       var url = cl.refreshTrendsNewUrl+"&callback=JSON_CALLBACK&endTime="+ endTime+"&wxid="+pro.wxid+"&reSet="+pro.reSet+"&tabId="+pro.tabId+"&catId="+pro.catId+"&page="+page;
	       console.log(url);
	       $http.jsonp(url).success(function (res) {
	    	   var pageTimes = (new Date()).valueOf();
	      	//处理成功的情况
	    	   if(res.code=="CL02046"){
	    		   if(!pro.reSet){
	    			   var items = res.result.newest;
	    			   console.log(items);
	    			   if(pro.tabId=="newest"){
				             for (var i = 0; i < items.length; i++) {
				            	 items[i].lazy = "lazy"+pageTimes;
				            	 pro.newest.push(items[i]);
				            	 
				            	 pro.page = +1;
				             }
	    			   }else if(pro.tabId=="newstop"){
	    				   for (var i = 0; i < items.length; i++) {
	    					   items[i].lazy = "lazy"+pageTimes;
	    					   pro.news_top.push(items[i]);
	    				   }
	    			   }
	    		   }else{
	    			   //重置
	    			    pro.newest = res.result.newest;
						pro.news_top = res.result.news_top;
						for (var i = 0; i < pro.newest.length; i++) {
							pro.newest[i].lazy = "lazy"+pageTimes;
						}
						for (var i = 0; i < pro.news_top.length; i++) {
							pro.news_top[i].lazy = "lazy"+pageTimes;
						}
	    		   }
	    		   trendsService.setNewest(pro.newest);
	    		   trendsService.setNewsTop(pro.news_top);
		      	}
	    	   setTimeout(function(){
	    		   lazyloadPage("lazy"+pageTimes);
	    	   },1000);
	    	   pro.busy = false;
	    	   pro.reSet = 0;
	    	   hideSpin();
	       });
	 	};
	 	$scope.trends.nextPageTop = function(){//上拉刷新
	 		if(this.tabId=="newest"){
	 			return false;
	 		}
	 		if(!isFooter()){
	 			if(!this.reSet){
	 				return ;
	 			}
	 		}
	 		if (this.busy){
	 			hideSpin();
	 			return;
	 		}
	 		this.busy = true;
	 		var pro = this;
	 		
	 		var endTime = "";
	 		if(!this.reSet){//简单刷新
	 			if(this.tabId=="newest"){
	 				var item = this.newest;
	 			}else if(this.tabId=="newstop"){
	 				var item = this.news_top;
	 			}
	 			if(item.length>0){
	 				site = parseInt(item.length)-1;
	 				var endTime = item[site].create_time;
	 			}else{
	 				var endTime = "";
	 			}
	 			
	 		}
	 		
	 		var url = cl.refreshTrendsNewUrl+"&callback=JSON_CALLBACK&endTime="+ endTime+"&wxid="+pro.wxid+"&reSet="+pro.reSet+"&tabId="+pro.tabId+"&catId="+pro.catId;
	 		$http.jsonp(url).success(function (res) {
	 			//处理成功的情况
	 			 var pageTimes = (new Date()).valueOf();
	 			if(res.code=="CL02046"){
	 				if(!pro.reSet){
	 					var items = res.result;
	 					if(pro.tabId=="newest"){
	 						for (var i = 0; i < items.length; i++) {
	 							items[i].lazy = "lazy"+pageTimes;
	 							pro.newest.push(items[i]);
	 						}
	 					}else if(pro.tabId=="newstop"){
	 						for (var i = 0; i < items.length; i++) {
	 							items[i].lazy = "lazy"+pageTimes;
	 							pro.news_top.push(items[i]);
	 						}
	 					}
	 				}else{
	 					pro.newest = res.result.newest;
	 					pro.news_top = res.result.news_top;
	 					for (var i = 0; i < pro.newest.length; i++) {
							pro.newest[i].lazy = "lazy"+pageTimes;
						}
						for (var i = 0; i < pro.news_top.length; i++) {
							pro.news_top[i].lazy = "lazy"+pageTimes;
						}
	 				}
	 				trendsService.setNewest(pro.newest);
	 				trendsService.setNewsTop(pro.news_top);
	 			}
	 			 setTimeout(function(){
		    		   lazyloadPage("lazy"+pageTimes);
		    	   },1000);
//	 			setTimeout("jq_load();",1000);
	 			pro.busy = false;
	 			pro.reSet = 0;
	 			hideSpin();
	 		});
	 	};
	 	$scope.trends.delNews = function(id,index){//上拉刷新
	 		if (this.busy)
			{
		       	 return;
	        }
		    this.busy = true;
		    showSpin();
			var url = cl.delNewsUrl+"&callback=JSON_CALLBACK&id="+id+"&wxid="+this.wxid;
			var pro = this;
			showConfirm("确定删除？",function()
			{
				 $http.jsonp(url).success(function (res) {
						if(res.code=="CL02048"){//删除成功
							showAlert("成功删除该帖子");
							if(pro.tabId=="newest"){
								pro.newest.splice(index,1);
								trendsService.setNewest(pro.newest);
							}else if(pro.tabId=="newstop"){
								pro.news_top.splice(index,1);
								trendsService.setNewsTop(pro.news_top);
							}
						}else{
							showAlert("删除帖子失败");
						}
						pro.busy = false;
						hideSpin();
				 })
			},function(){
				pro.busy = false;
				hideSpin();
			});
	 	};
	 	if(trendsService.newest.length<=0||trendsService.newest.length>=100){//缓存100个数据，超过则进入该页面要重新请求
 	 		$scope.trends.initTrends();
 	 	}else{
 	 		setTimeout('initBox();initClick();btnScroll();',1000);
 	 		hideSpin();
 	 	}
	 	
    };
    return {controller:controller,tpl:tpl};
});
